<template>
  <div class="recommend">
    <!-- 发布评论 -->
    <div class="desc">
      <div class="language">
        <textarea name
                  id
                  cols="30"
                  rows="10"
                  v-model="content"
                  @keydown.enter="pushCommend()"></textarea>
      </div>
      <div class="sub"
           @click="pushCommend()">评论</div>
    </div>
    <!-- 展示评论 -->
    <div class="content">
      <p>精彩评论</p>
      <div class="item"
           v-for="(item,index) in recommends"
           :key="index">
        <!-- 用户头像 -->
        <div class="icon">
          <img :src="item.user.avatarUrl"
               alt="">
        </div>
        <div class="mess">
          <div class="top">
            <span>{{item.user.nickname}}:</span>
            {{item.content}}
          </div>
          <!-- 评论时间 -->
          <div class="bottom">{{_formateDate(item.time)}}</div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import { formatDate } from 'assets/common/tool'
import { _pushCommend } from 'network/detail'
export default {
  name: 'Recommends',
  props: {
    recommends: {
      type: Array,
      default () {
        return []
      }
    },
    id: {
      type: String,
      default () {
        return ''
      }
    }
  },
  data () {
    return {
      content: ''
    }
  },
  methods: {
    // 格式化时间
    _formateDate (data) {
      return formatDate(new Date(data), 'yyyy年MM月dd日 hh:mm:ss')
    },
    pushCommend () {
      const cookie = this.$store.getters.getCookie;
      // 判断用户是否登录
      if (cookie !== '' && cookie !== null) {
        _pushCommend(cookie, this.id, this.content).then(res => {
          console.log(res);
          if (res.data.code === 200) {
            return this.$message.success('评论成功！')
          } else {
            return this.$message.error('评论失败')
          }
        })
      } else {
        return this.$message.warning('请先登陆')
      }
      this.content = ''
    }
  }
}
</script>

<style scoped>
.recommend {
  width: 100%;
  box-sizing: border-box;
  padding: 20px 0 10px 0;
  position: relative;
}
.recommend .desc {
  padding: 10px;
  height: 105px;
  background: #eee;
}
.recommend .desc .language {
  height: 60px;
}
.desc .language textarea {
  width: 100%;
  height: 100%;
  padding-left: 10px;
  outline-style: none;
  background: #fff;
  color: #000;
  border: none;
}
.sub {
  float: right;
  margin-top: 5px;
  cursor: pointer;
}
.recommend .content {
  width: 100%;
  font-size: 13px;
  color: var(--color-text-tint);
}
.content p {
  padding: 10px;
  color: #000;
  font-size: 16px;
  border-bottom: 1px solid #333;
}
.item {
  padding: 10px 0 10px 10px;
  border-bottom: 1px solid #ddd;
  display: flex;
  position: relative;
}
.icon {
  width: 50px;
  height: 50px;
  margin-right: 15px;
}
.icon img {
  width: 100%;
}
.mess {
  flex: 1;
}
.top {
  margin-bottom: 10px;
  color: #000;
}
.top span {
  color: #00aa85;
}
.bottom {
  position: absolute;
  bottom: 0px;
  font-size: 12px;
  color: #333;
}
</style>
